package org.example.examfx02.tool;

import javafx.collections.ObservableList;
import org.example.examfx02.entity.*;

import java.text.Collator;
import java.util.*;

public class SystemData {
    private static User curUser = null;
    private CourseSelectManager sc = CourseSelectManager.getInstance();
    private final static SystemData instance = new SystemData();
    private List<Course> courses;
    private UserGroup userGroup;
    public static SystemData getInstance(){
        return instance;
    }
    private SystemData(){
        this.courses = new ArrayList<>();
        this.userGroup = new UserGroup();
    }

    public CourseSelectManager getSc() {
        return sc;
    }
    public void setSc(CourseSelectManager sc) {
        this.sc = sc;
    }
    public List<Course> getCourses() {
        return courses;
    }
    public static User getCurUser() {
        return curUser;
    }
    public static void setCurUser(User curUser) {
        SystemData.curUser = curUser;
    }
    public Course getCourseById(String ID){
        for(int i = 0;i<courses.size();i++){
            if(courses.get(i).getId().equals(ID)){
                return courses.get(i);
            }
        }
        return null;
    }
    public void updateCourse(Course updatedCourse) {
        for (int i = 0; i < courses.size(); i++) {
            if (courses.get(i).getId().equals(updatedCourse.getId())) {
                // 替换原对象为修改后的对象
                courses.set(i, updatedCourse);
                break;
            }
        }
    }
    public ArrayList<String> getCourseID(){
        ArrayList<String> courseID = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            courseID.add(courses.get(i).getId());
        }
        return courseID;
    }
    public ArrayList<Course> FindCoursebyTeacher(String name) {
        ArrayList<Course> tcourse = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            Teacher t = instance.getUserGroup().getTeacherByID(courses.get(i).getTeacherId());
            if(t.getUsername().equals(name)){
                tcourse.add(courses.get(i));
            }
        }
        return tcourse;
    }
    public ArrayList<Course> FindCoursebyID(String ID) {
        ArrayList<Course> idcourse = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            if(courses.get(i).getId().equals(ID)){
                idcourse.add(courses.get(i));
            }
        }
        return idcourse;
    }
    public Course FindCourseId(String id){
        for(int i = 0;i<courses.size();i++){
            if(courses.get(i).getId().equals(id)){
                return courses.get(i);
            }
        }
        return null;
    }
    public Student FindStudentId(String id){
        List<Student> students = instance.getUserGroup().getStudents();
        for(int i = 0;i<students.size();i++){
            if(students.get(i).getId().equals(id)){
                return students.get(i);
            }
        }
        return null;
    }
    public ArrayList<Course> FindCoursebynum(Integer num) {
        ArrayList<Course> numcourse = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            if(courses.get(i).getChooseNum() == num){
                numcourse.add(courses.get(i));
            }
        }
        return numcourse;
    }
    // 按教师名称排序（字母顺序）
    public static void sortByTeacherName(ObservableList<Course> courses) {
        Collections.sort(courses, new Comparator<Course>() {
            @Override
            public int compare(Course c1, Course c2) {
                Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
                // 获取教师名称（处理null情况）
                Teacher t1 = instance.getUserGroup().getTeacherByID(c1.getTeacherId());
                Teacher t2 = instance.getUserGroup().getTeacherByID(c2.getTeacherId());
                String name1 = t1 != null ? t1.getUsername() : "";
                String name2 = t2 != null ? t2.getUsername() : "";
                return compare.compare(name1,name2);
            }
        });
    }
    // 按编号排序
    public static void sortByID(ObservableList<Course> courses) {
        Collections.sort(courses, new Comparator<Course>() {
            @Override
            public int compare(Course c1, Course c2) {
                String id1 = c1.getId();
                String id2 = c2.getId();
                return id1.compareTo(id2);
            }
        });
    }
    // 按选课人数排序
    public static void sortByNum(ObservableList<Course> courses) {
        Collections.sort(courses, new Comparator<Course>() {
            @Override
            public int compare(Course c1, Course c2) {
                int num1 = c1.getChooseNum();
                int num2 = c2.getChooseNum();
                return num1 - num2;
            }
        });
    }
    // 获取某老师授课的课程
    public List<Course> getCourseByTeacherId(String id){
        List<Course> list = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            if(id.equals(courses.get(i).getTeacherId())){
                list.add(courses.get(i));
            }
        }
        return list;
    }
    // 获取某学生上的课程
    public List<Course> getCourseByStudentId(String id){
        List<Course> list = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            if(courses.get(i).getType()==0){
                list.add(courses.get(i));
            }
        }
        for(int i = 0;i<sc.getScList().size();i++){
            if(sc.getScList().get(i).getStudentId().equals(id)){
                list.add(FindCourseId(sc.getScList().get(i).getCourseId()));
//                System.out.println(id);
            }
        }
        return list;
    }
    // 获取某学生选修的课
    public List<Course> getCourseChooseByStudentId(String id){
        List<Course> list = new ArrayList<>();
        for(int i = 0;i<sc.getScList().size();i++){
            if(sc.getScList().get(i).getStudentId().equals(id)){
                list.add(FindCourseId(sc.getScList().get(i).getCourseId()));
            }
        }
        return list;
    }
    // 获取选某课程的老师
    public List<Student> getSelectCourceStudent(String id){
        List<Student> list = new ArrayList<>();
        for(int i = 0;i<sc.getScList().size();i++){
            SelectCourse selectCourse = sc.getScList().get(i);
            if(selectCourse.getCourseId().equals(id)){
                list.add(FindStudentId(selectCourse.getStudentId()));
            }
        }
        return list;
    }
    // 获取选修课
    public List<Course> getCourseChoose(){
        List<Course> list = new ArrayList<>();
        for(int i = 0;i<courses.size();i++){
            if(courses.get(i).getType()==1){
                list.add(courses.get(i));
            }
        }
        return list;
    }
    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }
    public UserGroup getUserGroup() {
        return userGroup;
    }
    public void setUserGroup(UserGroup userGroup) {
        this.userGroup = userGroup;
    }
}
